<?php
/**
 * 订单的自动归档
    1、自动归档当前时间往前推三个月的订单
    2、每日凌晨定时任务自动处理
    3、调用系统归档记录，将数据全部丢到archive APP应用下
    4、通过查询的数据，每次归档500条，最终归档完成截至
 * @author liuhua@leemanpaper.com
 * @version $20180411
 */
$domain = $argv[1];

if (empty($domain)) {
    die('No Params');
}
set_time_limit(0);
require_once(dirname(__FILE__) . '/../../lib/init.php');
cachemgr::init(false);
$_SERVER['HTTP_HOST'] = $domain;

try{
    //指定归档日期
    $archive_time= strtotime("2018-01-01 00:00:00");
    $archivelib = kernel::single('archive_order');

    $db = kernel::database();
    $sql="SELECT count(*) order_total FROM sdb_ome_orders WHERE (`archive`='1' OR `status` in('dead')
            OR (pay_status='0' AND process_status in ('unconfirmed')) OR (is_fail='true' AND pay_status in ('0','1'))) AND createtime<".$archive_time."
            ORDER BY createtime asc limit 0,1;";
    $data_total = $db->select($sql);

    $fornumber=ceil($data_total[0]["order_total"]/500);
    error_log(date('Y-m-d H:i:s').'归档总数：【'.($data_total[0]["order_total"])."】循环次数：【".$fornumber."】\r\n", 3, PUBLIC_DIR . '/autobackuporder' . date('Y-m-d') . '.log');

    //根据返回本次归档总署，按次数执行自动归档代码
    for ($i=1; $i <=$fornumber ; $i++) 
    { 
        error_log(date('Y-m-d H:i:s').'循环次数：【'.$i.'】\r\n', 3, PUBLIC_DIR . '/autobackuporder' . date('Y-m-d') . '.log');
        $result = $archivelib->autoorderprocess($archive_time);
        error_log(date('Y-m-d H:i:s').'循环次数：【'.$i.'】成功\r\n', 3, PUBLIC_DIR . '/autobackuporder' . date('Y-m-d') . '.log');
    }
  


}catch (Exception $e) 
{
    error_log(date('Y-m-d H:i:s') . '发送数据/' . var_export($e->getMessage(), true), 3, PUBLIC_DIR . '/autobackuporder' . date('Y-m-d') . '.log');
}


